package com.sanpu.kxe.business.repay.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.repay.adapter.RepayChannelAdapter;
import com.sanpu.kxe.business.repay.contract.RepayChannelContract;
import com.sanpu.kxe.business.repay.contract.RepayConfig;
import com.sanpu.kxe.business.repay.presenter.RepayChannelPresenter;
import com.sanpu.kxe.business.repay.provider.RepayOpenProvider;
import com.sanpu.kxe.business.trade.bean.PayCollectionOutput;
import com.sanpu.kxe.jump.eventbus.RefreshChannelEvent;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.utils.DateUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;

/**
 * Description：还款渠道
 * Created by 薛勇军 on 2019/4/24.
 */

public class RepayChannelActivity extends BaseTitleActivity implements RepayChannelContract.View {

    @Bind(R.id.recyclerView)
    ListView recyclerView;
    @Bind(R.id.emptyView)
    View emptyView;

    private RepayChannelAdapter payAdapter;

    private RepayChannelContract.Presenter mPresenter;

    private BankInfoOutput infoOutput;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_repay_channel;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("还款通道");
        infoOutput = (BankInfoOutput) getIntent().getSerializableExtra(RepayConfig.INTENT_BANKINFO);
        payAdapter = new RepayChannelAdapter(null, mContext);
        recyclerView.setAdapter(payAdapter);

        mPresenter = new RepayChannelPresenter(this);
        if (infoOutput != null) {
            mPresenter.appCnlConfList(infoOutput.getBankId());
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final PayCollectionOutput collectionOutput =
                        ((RepayChannelAdapter) parent.getAdapter()).getItem(position);
                if (collectionOutput != null) {
                    if (TextUtils.equals("DISABLE", collectionOutput.getEnableStatus())) {
                        new GlobalDialog(mContext)
                                .title("温馨提示")
                                .content(collectionOutput.getDisableReason())
                                .show();
                    } else if (TextUtils.isEmpty(collectionOutput.getBindCardId())) {
                        mPresenter.onBindCard(collectionOutput.getAppCnlConfId(), infoOutput);
                    } else if (!DateUtils.isSupportPaymentTime(
                            collectionOutput.getTradeStartTime() + "," + collectionOutput.getTradeEndTime())) {
                        String hint = "暂时不能还款，还款时间段：(" + collectionOutput.getTradeStartTime() +
                                "-" + collectionOutput.getTradeEndTime() + ")。";
                        new GlobalDialog(mContext)
                                .content(hint)
                                .headerIcon(R.drawable.img_warm_prompt)
                                .show();
                    } else {
                        RepayOpenProvider.getInstance()
                                .openRefundActivity(mContext, collectionOutput, infoOutput);
                    }
                }
            }
        });
    }

    @Override
    public void showListDataView(List<PayCollectionOutput> payCollectionOutputs) {
        if (payCollectionOutputs != null && payCollectionOutputs.size() > 0) {
            emptyView.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            payAdapter.setNotifyData(payCollectionOutputs);
        } else {
            emptyView.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(RefreshChannelEvent event) {
        if (event != null) {
            mPresenter.appCnlConfList(infoOutput.getBankId());
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public void setPresenter(RepayChannelContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
